package com.yanqu.road.server.cmd.activity.climbtower;

import com.yanqu.road.entity.activity.climbtower.ClimbTowerConfig;
import com.yanqu.road.entity.activity.climbtower.ClimbTowerRoleInfo;
import com.yanqu.road.entity.activity.climbtower.UserClimbTowerData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ClimbTowerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.climbtower.ClimbTowerModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.climbtower.ClimbTowerMgr;
import com.yanqu.road.server.pb.ClimbTowerActivityPb;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CLIMB_TOWER_GET_DATA, desc = "获取数据")
public class ClimbTowerGetUserDataCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        // 没有活动
        if (!ActivityMgr.activityInShowTime(ClimbTowerMgr.getActivityInfo()) || ClimbTowerMgr.getConfig() == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        ClimbTowerProto.ClimbTowerGetUserDataRespMsg.Builder respMsg = ClimbTowerProto.ClimbTowerGetUserDataRespMsg.newBuilder();
        ClimbTowerModule module = player.getModule(ClimbTowerModule.class);
        UserClimbTowerData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        ClimbTowerConfig config = ClimbTowerMgr.getConfig();
        ClimbTowerRoleInfo roleInfo = config.getClimbTowerRoleInfo(userData.getExp());
        ClimbTowerProto.ClimbTowerUserDataTemp.Builder userBuilder = ClimbTowerActivityPb.buildUserData(userData, roleInfo, module.isInTopCell());
        ClimbTowerProto.ClimbTowerConfigSyncMsg.Builder configSync = ClimbTowerProto.ClimbTowerConfigSyncMsg.newBuilder();
        module.buildPassCellTempList(config, configSync, userData.getEvent().getPassId(), userData);
        respMsg.setRet(0);
        respMsg.setUserData(userBuilder);
        respMsg.addAllPass(configSync.getPassList());
        player.sendPacket(Protocol.U_CLIMB_TOWER_GET_DATA, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_CLIMB_TOWER_GET_DATA, ClimbTowerProto.ClimbTowerGetUserDataRespMsg.newBuilder().setRet(ret));
        }
    }
}
